package com.gogowan.agroenterprise.utils;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;


import com.gogowan.agroenterprise.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/*亲自优化的权限申请工具类 ，用起来可能比较别扭，将就用吧!*/
public class PermissionUtil {
    private Activity activity;
    private String[] perissionList;
    private String TitleStr, MstStr;
    private static int PERISSON_RESULT_CODE=10;
    private OnPerissionListener onPerissionListener;
    private int ShowCountrol=0;
    public PermissionUtil(Activity activity) {
        this.activity=activity;
    }
    //申请权限提示框
    private void setPopupWindow(String TitleStr, String MstStr, final String[] p){
        final PopupWindowUtil popupWindowUtil=new PopupWindowUtil(activity);
        popupWindowUtil.setContextView(R.layout.activity_message);
        popupWindowUtil.setType(0,0);
        TextView title_Tv=popupWindowUtil.getView().findViewById(R.id.message_title_Tv);
        TextView msg_Tv=popupWindowUtil.getView().findViewById(R.id.message_str_Tv);
        title_Tv.setText(TitleStr);
        msg_Tv.setText(MstStr);
        popupWindowUtil.getView().findViewById(R.id.message_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCompat.requestPermissions(activity, p, PERISSON_RESULT_CODE);
                popupWindowUtil.getPopupWindow().dismiss();
            }
        });
        popupWindowUtil.getView().findViewById(R.id.message_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindowUtil.getPopupWindow().dismiss();
            }
        });
        popupWindowUtil.show(Gravity.BOTTOM,0,0);
    }
    //判断权限并申请
    public PermissionUtil getPerission(String TitleStr, String MstStr, String... ManifestPermission) {
        perissionList=ManifestPermission;
        this.TitleStr=TitleStr;
        this.MstStr=MstStr;
        List<String> OverPerissions=new ArrayList<>();
        List<String> NotPerissions=new ArrayList<>();
        String[] p=new String[]{};
        for (int i=0;i<perissionList.length;i++){
            String perission=ManifestPermission[i];
            if (ContextCompat.checkSelfPermission(activity, perission) != PackageManager.PERMISSION_GRANTED){
                p = Arrays.copyOf(p, p.length+1);
                p[p.length-1] =perission;
                NotPerissions.add(perission);
            }else {
                OverPerissions.add(perission);
            }
        }
        if (ShowCountrol==0){
            if (p.length>0){
                setPopupWindow(TitleStr,MstStr,p);
            }else {
                onPerissionListener.OnAllGetResult(OverPerissions);
            }
        }else if(ShowCountrol==1){
            if (OverPerissions.size()==perissionList.length){
                onPerissionListener.OnAllGetResult(OverPerissions);
            }else {
                onPerissionListener.OnThrough(OverPerissions);
                onPerissionListener.OnError(NotPerissions);
            }
        }
        return this;
    }
    //权限申请回调
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        ShowCountrol=1;
        getPerission(TitleStr,MstStr,perissionList);
    }
    //设置权限申请结果统计监听
    public PermissionUtil setOnPerissionLisner(OnPerissionListener onPerissionListener){
        ShowCountrol=0;
        this.onPerissionListener=onPerissionListener;
        return this;
    }
    //权限申请结果统计监听
    public interface OnPerissionListener{
        void OnAllGetResult(List<String> perission_names);//全部通过回调
        void OnThrough(List<String> perission_names);// 通过的权限回调
        void OnError(List<String> perission_names);//未通过的权限回调
    }
    //权限枚举
    public enum Permission{
        CAMERA_PERMISSION(Manifest.permission.CAMERA),
        READ_CALENDAR_PERMISSION(Manifest.permission.READ_CALENDAR),
        READ_EXTERNAL_STORAGE(Manifest.permission.READ_EXTERNAL_STORAGE),
        WRITE_EXTERNAL_STORAGE(Manifest.permission.WRITE_EXTERNAL_STORAGE),
        WRITE_CALENDAR_PERMISSION(Manifest.permission.WRITE_CALENDAR),
        READ_COUNTACTS_PERMISSION(Manifest.permission.READ_CONTACTS),
        WRITE_COUNTACTS_PERMISSION(Manifest.permission.WRITE_CONTACTS),
        ACCESS_FINE_LOCATION_PERMISSION(Manifest.permission.ACCESS_FINE_LOCATION),
        ACCESS_COARSE_LOCATION_PERMISSION(Manifest.permission.ACCESS_COARSE_LOCATION);
        private final String value;
        public String getValue() {
            return value;
        }
        Permission(String value){
            this.value=value;
        }
    }
}
